package com.tanhua.server.service;


import com.alibaba.fastjson.JSONObject;
import com.tanhua.dubbo.api.SoulTableApi;
import com.tanhua.model.mongo.Report;
import com.tanhua.model.vo.TestQuestionsvo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.utils.Constants;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TestSoulService {

    @DubboReference
    private SoulTableApi soulTableApi;

    @Autowired
    private RedisTemplate redisTemplate;

    public List<TestQuestionsvo> testsoul() {
        //调用api查询到灵魂表的数据
        return soulTableApi.fanall();
    }

    /*public void tiJiaoWenJuan(JSONArray answers) {
        //调用api查询到每一个题的详情
        List<TestQuestions> testQuestionslist = soulTableApi.getti(list);
    }*/

    public void tiJiaoWenJuan(List<JSONObject> list) {
        //创建map集合添加数据
        Map<String, String> map = new HashMap<>();
        ArrayList<Map> answers = new ArrayList<>();//作答
        //循环list得到值
        for (JSONObject jsonObject : list) {
            String questionId = jsonObject.getString("questionId");//试题编号
            String optionId = jsonObject.getString("optionId");//选项编号
            map.put("questionId", questionId);
            map.put("optionId", optionId);
            answers.add(map);
        }
        //调用api查询到所有的选择对应的分数
        List<String> defen = soulTableApi.getti(answers);
        Long zongfen = 0L;
        //循环出来每一个得分计算出来总的得分
        for (int i = 0; i < defen.size(); i++) {
            Long s = Long.valueOf(defen.get(i));
            zongfen += s;
        }
        //将得分存入数据库
        soulTableApi.addreport(zongfen);
    }

    public Report ChaXun(String id) {
        //调用api查询到数据
        Report report = soulTableApi.ChaXun(id);
        return report;
    }


}
